//
//  QiangZheKouViewController.m
//  TravelYo
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 TravelYo. All rights reserved.
//

#import "QiangZheKouViewController.h"

@interface QiangZheKouViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,  UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *leiArray;

@property (nonatomic, strong) NSMutableArray *dataArray;




@end

@implementation QiangZheKouViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    [self addSubViews];
    
    [self setUpdataWithUrl:kAllLeiAPI];
    
    [self refresh]; // 刷新数据
    // Do any additional setup after loading the view.
}


- (void)refresh
{
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [self.collectionView.header beginRefreshing];
    
    
    self.collectionView.footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [self.collectionView.footer beginRefreshing];
}

- (void)headerRefresh
{
    _dataArray = nil;
    [self setUpdataWithUrl:kAllLeiAPI];
    [self.collectionView.header endRefreshing];
}

- (void)footerRefresh
{
    
}


- (void)addSubViews
{
    self.leiArray = @[kAllLeiAPI, kJipiaoLeiAPI, kjiudianLeiAPI, kZiyouLeiAPI, kqianzhengLeiAPI, kyoulunLeiAPI];
    DaoHangView *daoHang = [[DaoHangView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    // daoHang.backgroundColor = [UIColor purpleColor];
    for (UIButton *button in daoHang.subviews) {
        [button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    [self.view addSubview:daoHang];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5; // 行间距
    layout.minimumLineSpacing = 5; // 列间距
    layout.itemSize = CGSizeMake((kScreenWidth - 15) / 2, (kScreenHeight - 20) / 3 ); // items 的大小
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layout.headerReferenceSize = CGSizeMake(0, 0);
    
    
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 34, kScreenWidth, kScreenHeight - 64) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    // 设置代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.bounces = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_collectionView];
    
    
    // 注册
    [_collectionView registerClass:[KanZheKouCollectionCell class] forCellWithReuseIdentifier:@"KanzhekouCell"];
}

#pragma mark --- collectionView 代理方法



// items数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

// 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// items
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KanZheKouCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KanzhekouCell" forIndexPath:indexPath];
    if (self.dataArray.count != 0) {
        QiangZheKouModel *model = self.dataArray[indexPath.row];
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.pic]];
        cell.titleLabel.text = model.title;
        cell.priceOffLabel.text = model.lastminute_des;
        cell.monthsLabel.text = model.departureTime;
        cell.priceLabel.text = model.buy_price;
    }
    // cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}




// items 点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArray.count != 0) {
        QiangZheKouModel *model = self.dataArray[indexPath.row];
        ImageXQViewController *imageXQ = [[ImageXQViewController alloc] init];
        imageXQ.url = model.url;
        // 跳转的时候隐藏navigationBar
        imageXQ.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:imageXQ animated:YES];
    }
    
}


// 点击button 刷新界面
- (void)actionButton:(UIButton *)button
{
    self.dataArray = nil;
    [self setUpdataWithUrl:self.leiArray[button.tag - 10]];
    
    [UIView animateWithDuration:1.0 animations:^{
        button.backgroundColor = kMainColor;
        [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    } completion:^(BOOL finished) {
        button.backgroundColor  = [UIColor clearColor];
        [button setTitleColor:kMainColor forState:(UIControlStateNormal)];
    }];
    
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

// 解析数据
-(void)setUpdataWithUrl:(NSString *)url
{
    [[TYDataManager shareManager] requestDataWithUrl:url block:^(NSData *data){
        NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *diction = dict[@"data"];
        NSArray *array = diction[@"lastminutes"];
        for (NSDictionary *dic in array) {
            QiangZheKouModel *model = [[QiangZheKouModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        
        [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
        [self.collectionView reloadData];
    }];
    ;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
